package com.sian.security.utils;

import java.security.MessageDigest;

import org.apache.log4j.Logger;

public class MD5 {
	private static Logger logger = org.apache.log4j.LogManager.getLogger(MD5.class);
	public static String encode(String password)
	{
		byte[] unencodedPassword = password.getBytes(); 
		MessageDigest md = null;
		try
		{
			md = MessageDigest.getInstance("MD5");
		}
		catch (Exception e)
		{
			logger.error(e.getMessage(), e);
			throw new RuntimeException(e.getMessage());
		}
		md.reset();
		md.update(unencodedPassword);
		byte[] encodedPassword = md.digest();
		StringBuffer buf = new StringBuffer();
		for (int i = 0; i < encodedPassword.length; i++)
		{
			if ((encodedPassword[i] & 0xff) < 0x10)
			{
				buf.append("0");
			}
			buf.append(Long.toString(encodedPassword[i] & 0xff, 16));
		}
		return buf.toString();
	}
}
